package com.zy.cat.controller.dev.v1;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.dao.Dao;
import com.zy.cat.entity.dev.CatDev5088InviteIncomeHistory;
import com.zy.cat.entity.dev.CatDevUserInfo;
import com.zy.cat.entity.dev.CatDevWeInfo;
import com.zy.cat.entity.dev.repository.CatDev5088InviteIncomeHistoryRepository;
import com.zy.cat.entity.dev.resp.CatDev5088InviteInfoResp;
import com.zy.cat.entity.dev.resp.CatDev5088InviteInfoResp.InviteRemit;
import com.zy.cat.entity.dev.resp.CatDevHCInviteHistoryResp;
import com.zy.cat.entity.dev.resp.CatDevHCInviteInfoResp;
import com.zy.cat.entity.dev.resp.CatDevInviteTeamInfoResp;
import com.zy.cat.entity.dev.resp.CatDevInviteUrlInfoResp;
import com.zy.cat.entity.dev.resp.CatDevOrderResp;
import com.zy.cat.entity.dev.resp.CatDevRemitRecordResp;
import com.zy.cat.entity.dev.resp.CatDevTeamRebateResp;
import com.zy.cat.entity.dev.resp.CatDevUserInfoResp;
import com.zy.cat.entity.dev.resp.CatDevUserInfoResp.TLJInfo;
import com.zy.cat.entity.dev.resp.CatDevZJInviteDetailedResp;
import com.zy.cat.entity.dev.resp.CatDevZJInviteInfoResp;
import com.zy.cat.entity.resp.Pages;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.entity.resp.ResultPage;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatDevInviteService;
import com.zy.cat.service.CatDevService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "9catDevUserInvite", tags = { "9趣猫微信邀请好友开放API" }, description = "9趣猫微信邀请好友开放接口")
@RestController
@RequestMapping("/v1/user/invite")
@Slf4j
public class CatDevInviteController {

	@Autowired
	CatDevInviteService catDevInviteService;

	@Autowired
	CacheService cacheService;

	@Autowired
	CatDevService catDevService;

	@Autowired
	Dao<CatDevHCInviteInfoResp> hcInviteInfoRespDao;

	@Autowired
	CatDev5088InviteIncomeHistoryRepository catDev5088InviteIncomeHistoryRepository;

	@Autowired
	Dao<Integer> intDao;

	@Autowired
	Dao<Long> longDao;

	@PostMapping("/url")
	@ApiOperation(position = 1, value = "获取用户邀请好友链接", consumes = "application/x-www-form-urlencoded", notes = "作为微信分享后访问的地址", response = RSPE.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utype", value = "链接类型（0.正常邀请、1.互拆红包邀请、2.5088邀请、3.淘礼金邀请）", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "ctype", value = "应用类型（0.九趣猫、1.淘淘集）", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_utype_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_utype_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getInviteUrl(Integer rtype, Integer utype,Integer ctype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (utype == null || (utype != 0 && utype != 1 && utype != 2 && utype !=3 )) {
			return new RSPE<Object>(-3, "链接类型参数错误！");
		}
		if (ctype == null || (ctype != 0 && ctype != 1 && ctype != 2)) {
			return new RSPE<Object>(-3, "应用类型参数错误！");
		}
		Callable<RSPE<Map<String, String>>> result = new Callable<RSPE<Map<String, String>>>() {
			@Override
			public RSPE<Map<String, String>> call() throws Exception {
				Map<String, String> inviteUrl = catDevInviteService.getInviteUrl(rtype, utype,ctype, pid);
				if (inviteUrl == null) {
					return new RSPE<Map<String, String>>(-0, "用户信息错误！");
				}
				return new RSPE<Map<String, String>>(inviteUrl);
			}
		};
		return result;
	}

	@PostMapping("/url/init")
	@ApiOperation(position = 2, value = "点击邀请链接，记录邀请信息", notes = "微信分享后访问地址，获取用户信息后回调此接口", response = RSPE.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "邀请人PID", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "ytype", value = "邀请类型（0.正常邀请、1.互拆红包邀请、2.5088邀请、3.淘礼金邀请）", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "code", value = "被邀请人微信登录CODE", example = "UID_39D970F3F2DFE55FE6BAC85B79633B24", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_code_ytype_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_code_ytype", MD5 = "token")
	public Object inviteUrlInit(Integer rtype, Integer ytype, String pid, String code, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (ytype == null || (ytype != 0 && ytype != 1 && ytype != 2 && ytype!=3)) {
			return new RSPE<Object>(-3, "邀请类型参数错误！");
		}
		CatDevWeInfo weInfo = cacheService.getWxInfoByCode(rtype, code, 101, "", "");
		if (weInfo == null || StringUtils.isBlank(weInfo.getUnionid())) {
			return new RSPE<CatDevUserInfoResp>(-3, "微信unionid获取失败！请重试或联系客服！");
		}
		Callable<RSPE<Object>> result = new Callable<RSPE<Object>>() {
			@Override
			public RSPE<Object> call() throws Exception {
				Map<String, Object> map = catDevInviteService.setInviteInitInfo(rtype, ytype, pid, weInfo.getUnionid());
				return new RSPE<Object>((int) map.get("code"), (String) map.get("msg"));
			}
		};
		return result;
	}

	@PostMapping("/5088/info")
	@ApiOperation(position = 3, value = "获取5088邀请信息", notes = "进入5088邀请内页的所有信息", response = CatDev5088InviteInfoResp.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object get5088InviteInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<CatDev5088InviteInfoResp>> result = new Callable<RSPE<CatDev5088InviteInfoResp>>() {
			@Override
			public RSPE<CatDev5088InviteInfoResp> call() throws Exception {
				return new RSPE<CatDev5088InviteInfoResp>(catDevInviteService.get5088InviteInfo(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/5088/remit")
	@ApiOperation(position = 3, value = "5088邀请者申请提现", notes = "5088邀请内页申请提现", response = CatDev5088InviteInfoResp.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "rcode", value = "提现金额（分）", example = "500", required = true, dataType = "long", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object set5088Inviteremit(Integer rtype, Long rcode, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (rcode == null || (rcode != 500 && rcode != 1000 && rcode != 2000 && rcode != 5000 && rcode != 10000)) {
			return new RSPE<Object>(-3, "提现金额错误！");
		}
		/* 查看用户微信绑定信息 */
		CatDevUserInfo dev =catDevService.getCatDevUserInfoByPid(pid);
		/* 判断是否关注公众号 */
		Integer isFocusWeChat = catDevService.getIsFocusWeChat(rtype, pid,
				dev.getUnionid());
		if (isFocusWeChat == 0) {
			return new RSPE<CatDevRemitRecordResp>(-6, "未关注公众号,请先关注[九趣猫公]众号！");
		}
		/* 查看邀请好友奖励金今日是否提现 */
		boolean isRemit = cacheService.getTodayCatDevRemitInfoResp(pid, 5);
		if (isRemit) {
			return new RSPE<Boolean>(-7, "每日仅能提现一次！");
		}
		long inviteIncome = catDev5088InviteIncomeHistoryRepository.getYIncome(pid);

		log.info("-{}-账户余额->{}", pid, inviteIncome);
		if (inviteIncome < rcode) {
			return new RSPE<Object>(-4, "账户余额不足！");
		}

		List<InviteRemit> rlist = cacheService.getCatDev5088InviteRemitinfo();
		if (rlist != null && !rlist.isEmpty()) {
			for (InviteRemit inviteRemit : rlist) {
				if (inviteRemit.getIncome() == rcode && inviteRemit.getLcount() < 1) {
					return new RSPE<Object>(-5, "红包补货中，请稍后重试！");
				}
			}
		}

		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				
				CatDevRemitRecordResp remit = catDevService.remit(rtype,1, pid, 0, 5, dev.getZaccount(),
						dev.getZrealname(), rcode);
				if(remit!=null) {
					return new RSPE<Boolean>(1,"提现成功",true);	
				} 
				return new RSPE<Boolean>(0,"提现失败",false);	
			}
		};
		return result;
	}

	@PostMapping("/zj/info")
	@ApiOperation(position = 3, value = "获取用户直接邀请信息", notes = "进入邀请好友菜单内的所有信息", response = CatDevZJInviteInfoResp.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getZJInviteInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<CatDevZJInviteInfoResp>> result = new Callable<RSPE<CatDevZJInviteInfoResp>>() {
			@Override
			public RSPE<CatDevZJInviteInfoResp> call() throws Exception {
				return new RSPE<CatDevZJInviteInfoResp>(catDevInviteService.getZJInviteInfo(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/hc/info")
	@ApiOperation(position = 4, value = "获取用户互拆邀请信息", notes = "进入邀请好友菜单内的所有信息", response = CatDevHCInviteInfoResp.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getHCInviteInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}

		Callable<RSPE<CatDevHCInviteInfoResp>> result = new Callable<RSPE<CatDevHCInviteInfoResp>>() {
			@Override
			public RSPE<CatDevHCInviteInfoResp> call() throws Exception {
				return new RSPE<CatDevHCInviteInfoResp>(catDevInviteService.getHCInviteInfo(rtype, pid));
			}
		};
		return result;
	}

	@PostMapping("/hc/open/new")
	@ApiOperation(position = 5, value = "开启新一轮互拆红包活动", notes = "开启新一轮互拆红包", response = RSPE.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object openNewHCInviteInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		/*
		 * 已达标未体现——>null 未达标未体现-->null 已达标已提现
		 */
		// 判断本次互拆活动是否已达标
		List<CatDevHCInviteInfoResp> inviteInfoResp = hcInviteInfoRespDao.list(new CatDevHCInviteInfoResp(pid),
				"isFinishHcTask");
		if (inviteInfoResp != null && !inviteInfoResp.isEmpty()) {
			// 判断是否提现
			if (catDevInviteService.getHCInviteRimit(rtype, pid) == null) {
				return new RSPE<Boolean>(-9, "本轮互拆活动未提现！");
			}
		}

		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				Boolean isOpeNew = catDevInviteService.openNewHCInviteInfo(rtype, pid);
				if (isOpeNew) {
					return new RSPE<Boolean>(1, "开启成功");
				}
				return new RSPE<Boolean>(0, "开启失败");
			}
		};
		return result;
	}

	@PostMapping("/hc/draw")
	@ApiOperation(position = 5, value = "获取本次互拆红包邀请奖励信息", notes = "", response = CatDevHCInviteHistoryResp.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "ltype", value = "来源(0.个人中心、1.刮刮卡、2.神奇便利店、3.微信分享)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object setHCInviteInfo(Integer rtype, Integer ltype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (ltype == null || (ltype < 0 && ltype > 3)) {
			return new RSPE<Object>(-3, "来源参数错误！");
		}
		Callable<RSPE<CatDevHCInviteHistoryResp>> result = new Callable<RSPE<CatDevHCInviteHistoryResp>>() {
			@Override
			public RSPE<CatDevHCInviteHistoryResp> call() throws Exception {
				return new RSPE<CatDevHCInviteHistoryResp>(catDevInviteService.setHCInviteInfo(rtype, ltype, pid));
			}
		};
		return result;
	}

	@PostMapping("/zj/remit")
	@ApiOperation(value = "用户直接邀请金额提现", position = 6, notes = "用户提现直接邀请新用户赚取的奖励金，每日仅能提现一次", consumes = "application/x-www-form-urlencoded", response = CatDevRemitRecordResp.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "qtype", value = "取款类型（0.微信、1.支付宝）", example = "0", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "tmoney", value = "提款金额(分)", example = "100", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_tmoney_qtype_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken_tmoney_qtype_utoken", MD5 = "token", checkSingle = true)
	public Object zjRemit(Integer rtype, String pid, Integer qtype, Integer tmoney, String uuid, String utoken,
			String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (qtype == null || qtype < 0 || qtype > 1) {
			return new RSPE<Boolean>(-4, "取款类型不符合规范！");
		}
		if (qtype == 1) {
			return new RSPE<Boolean>(-20, "暂不支持支付宝提现！");
		}

		if (tmoney == null || tmoney <= 0) {
			return new RSPE<Boolean>(-5, "提款金额不符合规范！");
		}

		if (tmoney > 20 * 100) {
			return new RSPE<Boolean>(-9, "单次提款金额不得超过20元！");
		}
		/* 判断是否关注公众号 */
		Integer isFocusWeChat = catDevService.getIsFocusWeChat(rtype, pid,
				catDevService.getCatDevUserInfoByPid(pid).getUnionid());
		if (qtype == 0 && isFocusWeChat == 0) {
			return new RSPE<CatDevRemitRecordResp>(-12, "未关注公众号,请先关注九趣猫公众号！");
		}
		/* 查看用户微信绑定信息 */
		CatDevUserInfoResp dev = cacheService.getCatDevUserLoginInfo(pid);
		if (dev == null) {
			return new RSPE<Boolean>(-7, "提款用户未不存在！");
		}
		// 当取款类型为支付宝时判断是否已绑定支付宝
		if (qtype == 1 && (StringUtils.equals(dev.getZaccount(), "-") || StringUtils.equals(dev.getZrealname(), "-"))) {
			return new RSPE<Boolean>(-6, "未绑定支付宝账号！");
		}

		/* 查看邀请好友奖励金今日是否提现 */
		boolean isRemit = cacheService.getTodayCatDevRemitInfoResp(pid, 1);
		if (isRemit) {
			return new RSPE<Boolean>(-3, "每日仅能提现一次！");
		}
		/* 获取用户邀请所得红包 */
		if (tmoney > cacheService.getInviteNewUserIncome(pid)) {
			return new RSPE<Boolean>(-8, "提现金额必须小于或等于账户可提现金额！");
		}
		/* 判断本次提现金额与以往累加提现是否大于10元 */
		Long income = longDao.selectOne(new CatDevZJInviteInfoResp(pid), "getZJCumulativeRemitIncome");
		// 判断账号是否异常
		if (dev.getUtype() == -1) {
			// 用户类型为异常用户无法正常提现
			return new RSPE<CatDevRemitRecordResp>(-19, "账号行为异常无法提现");
		}

		// 如果累计大于10元，判断是否实名认证
		if ((income + tmoney) >= 10 * 100) {
			// 判断账户认证审核状态
			if (dev.getFstatus() == 0) {
				return new RSPE<CatDevRemitRecordResp>(-10, "未进行实名认证，请先进行实名认证！");
			} else if (dev.getFstatus() == 1) {
				return new RSPE<CatDevRemitRecordResp>(-14, "正在审核实名认证信息！");
			} else if (dev.getFstatus() == -1) {
				// 查询审核失败原因
				CatDevUserInfo info = catDevService.getCatDevUserInfoByPid(pid);
				return new RSPE<CatDevRemitRecordResp>(-15, info.getInfo());
			}
		}
		// 如果提款金额大于10元，判断是否绑定手机号
		if (tmoney >= 10 * 100 && StringUtils.isBlank(dev.getPhone())) {
			return new RSPE<CatDevRemitRecordResp>(-21, "提现需进行手机号绑定，请先进行手机号绑定！");
		}

		Callable<RSPE<CatDevRemitRecordResp>> result = new Callable<RSPE<CatDevRemitRecordResp>>() {
			@Override
			public RSPE<CatDevRemitRecordResp> call() throws Exception {
				CatDevRemitRecordResp remit = catDevService.remit(rtype,1, pid, qtype, 1, dev.getZaccount(),
						dev.getZrealname(), tmoney);
				if (remit != null) {
					return new RSPE<CatDevRemitRecordResp>(remit);
				}
				return new RSPE<CatDevRemitRecordResp>(0, "提现失败");
			}
		};
		return result;
	}

	@PostMapping("/hc/remit")
	@ApiOperation(value = "用户互拆红包收入提现", position = 7, notes = "用户提现直接邀请新用户赚取的奖励金，每日仅能提现一次", consumes = "application/x-www-form-urlencoded", response = CatDevRemitRecordResp.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "qtype", value = "取款类型（0.微信、1.支付宝）", example = "0", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_qtype_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken_qtype_utoken", MD5 = "token", checkSingle = true)
	public Object hcRemit(Integer rtype, String pid, Integer qtype, String uuid, String utoken, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		if (qtype == null || qtype < 0 || qtype > 1) {
			return new RSPE<Boolean>(-3, "取款类型不符合规范！");
		}
		if (qtype == 1) {
			return new RSPE<Boolean>(-20, "暂不支持支付宝提现！");
		}
		/* 查看用户微信绑定信息 */
		CatDevUserInfoResp dev = cacheService.getCatDevUserLoginInfo(pid);
		if (dev == null) {
			return new RSPE<Boolean>(-5, "提款用户未不存在！");
		}
		/* 判断是否关注公众号 */
//		Integer isFocusWeChat = catDevService.getIsFocusWeChat(rtype, pid, catDevService.getCatDevUserInfoByPid(pid).getUnionid());
//		if (qtype==0 && isFocusWeChat==0) {
//			return new RSPE<CatDevRemitRecordResp>(-12, "未关注公众号,请先关注九趣猫公众号！");
//		}
		// 当取款类型为支付宝时判断是否已绑定支付宝
		if (qtype == 1 && (StringUtils.equals(dev.getZaccount(), "-") || StringUtils.equals(dev.getZrealname(), "-"))) {
			return new RSPE<Boolean>(-6, "未绑定支付宝账号！");
		}

		/* 查看邀请好友奖励金今日是否提现 */
		Integer count = intDao.selectOne(new CatDevRemitRecordResp(pid), "getHCTodayRemitCount");
		if (count > 0) {
			return new RSPE<Boolean>(-7, "每日仅能提现一次！");
		}

		/*
		 * 已达标未体现——>null 未达标未体现-->null 已达标已提现
		 */
		// 判断本次互拆活动是否已达标
		List<CatDevHCInviteInfoResp> inviteInfoResp = hcInviteInfoRespDao.list(new CatDevHCInviteInfoResp(pid),
				"isFinishHcTask");
		if (inviteInfoResp == null || inviteInfoResp.isEmpty()) {
			return new RSPE<Boolean>(-9, "本轮互拆活动未达到10元！");
		}
		if (catDevInviteService.getHCInviteRimit(rtype, pid) != null) {
			return new RSPE<Boolean>(-8, "本轮互拆红包已领取！");
		}
		// 判断是否绑定手机号
		if (StringUtils.isBlank(dev.getPhone())) {
			return new RSPE<CatDevRemitRecordResp>(-21, "提现需进行手机号绑定，请先进行手机号绑定！");
		}

		// 判断账号是否异常
		if (dev.getUtype() == -1) {
			// 用户类型为异常用户无法正常提现
			return new RSPE<CatDevRemitRecordResp>(-19, "账号行为异常无法提现");
		}

		Callable<RSPE<CatDevRemitRecordResp>> result = new Callable<RSPE<CatDevRemitRecordResp>>() {
			@Override
			public RSPE<CatDevRemitRecordResp> call() throws Exception {
				CatDevRemitRecordResp remit = catDevInviteService.remitHC(rtype, pid, qtype, dev.getZaccount(),
						dev.getZrealname());
				if (remit != null) {
					return new RSPE<CatDevRemitRecordResp>(remit);
				}
				return new RSPE<CatDevRemitRecordResp>(0, "提现申请失败");
			}
		};
		return result;
	}

	@PostMapping("/zj/reward/detailed")
	@ApiOperation(position = 8, value = "获取用户直接邀请奖励明细", notes = "获取用户直接邀请奖励明细", response = CatDevZJInviteDetailedResp.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getZJRewardDetailed(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<CatDevZJInviteDetailedResp>> result = new Callable<RSPE<CatDevZJInviteDetailedResp>>() {
			@Override
			public RSPE<CatDevZJInviteDetailedResp> call() throws Exception {
				return new RSPE<CatDevZJInviteDetailedResp>(catDevInviteService.getZJRewardDetailed(rtype, pid));
			}
		};
		return result;
	}
	
	@PostMapping("/zj/reward/detailed/tlj")
	@ApiOperation(position = 8, value = "获取用户邀请淘礼金奖励明细", notes = "获取用户邀请淘礼金奖励明细", response = TLJInfo.class, consumes = "application/x-www-form-urlencoded")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getZJTljDetailed(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<List<TLJInfo>>> result = new Callable<RSPE<List<TLJInfo>>>() {
			@Override
			public RSPE<List<TLJInfo>> call() throws Exception {
				return new RSPE<List<TLJInfo>>(catDevInviteService.getZJTljDetailed(rtype, pid));
			}
		};
		return result;
	}


	@PostMapping("/open/url")
	@ApiOperation(position = 9, value = "获取用户让好友打开九趣猫的链接", consumes = "application/x-www-form-urlencoded", notes = "获取用户让好友打开九趣猫的链接", response = RSPE.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "itype", value = "提醒任务类型(1.第一天打开、2.第二天打开、3.第三天打开、4、完成一笔订单)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_itype_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_itype_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getOpenAppUrl(Integer rtype, Integer itype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<Map<String, String>>> result = new Callable<RSPE<Map<String, String>>>() {
			@Override
			public RSPE<Map<String, String>> call() throws Exception {
				Map<String, String> inviteUrl = catDevInviteService.getOpenAppUrl(rtype, itype, pid);
				if (inviteUrl == null) {
					return new RSPE<Map<String, String>>(-0, "用户信息错误！");
				}
				return new RSPE<Map<String, String>>(inviteUrl);
			}
		};
		return result;
	}

	@PostMapping("/url/inviter/info")
	@ApiOperation(position = 10, value = "获取邀请链接中邀请人信息", consumes = "application/x-www-form-urlencoded", notes = "获取邀请链接中邀请人信息", response = RSPE.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "key", value = "链接中携带的参数", example = "3DOTYzNjI5NTYwMV8wMA", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "appId", value = "应用ID(0.九趣猫(默认)、1.淘淘集)", example = "chan101", required = false, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_key_uuid_key_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_key_uuid_key_appId", MD5 = "token")
	public Object getUrlInviterInfo(Integer rtype, @RequestParam(required = false, defaultValue = "0") Integer appId,
			String key, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<CatDevInviteUrlInfoResp>> result = new Callable<RSPE<CatDevInviteUrlInfoResp>>() {
			@Override
			public RSPE<CatDevInviteUrlInfoResp> call() throws Exception {
				CatDevInviteUrlInfoResp inviteUrl = catDevInviteService.getUrlInviterInfo(rtype, appId, key);
				if (inviteUrl == null) {
					return new RSPE<CatDevInviteUrlInfoResp>(-0, "获取有邀请人信息失败,请重试！");
				}
				return new RSPE<CatDevInviteUrlInfoResp>(inviteUrl);
			}
		};
		return result;
	}
	
	@PostMapping("/team/info")
	@ApiOperation(position = 9, value = "获取用户所邀请的好友团队信息", consumes = "application/x-www-form-urlencoded", notes = "获取用户所邀请的好友团队信息", response = CatDevInviteTeamInfoResp.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_uuid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getMyTeamInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-2, "应用平台参数错误！");
		}
		Callable<RSPE<CatDevInviteTeamInfoResp>> result = new Callable<RSPE<CatDevInviteTeamInfoResp>>() {
			@Override
			public RSPE<CatDevInviteTeamInfoResp> call() throws Exception {
				CatDevInviteTeamInfoResp resp = catDevInviteService.getMyTeamInfo(rtype, pid);
				if (resp == null) {
					return new RSPE<CatDevInviteTeamInfoResp>(-0, "用户信息错误！");
				}
				return new RSPE<CatDevInviteTeamInfoResp>(resp);
			}
		};
		return result;
	}
	
	@PostMapping("/team/list")
	@ApiOperation(position = 9, value = "获取用户所邀请的好友团队订单信息", consumes = "application/x-www-form-urlencoded", notes = "获取用户所邀请的好友团队订单信息", response = CatDevTeamRebateResp.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
		@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
		@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
		@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
		@ApiImplicitParam(name = "token", value = "MD5(rtype_pid_utoken_uuid_uuid_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_pid_utoken_uuid_uuid", MD5 = "token")
	public Object list(Integer rtype, String pid, String utoken, String uuid, String token,
			Pages pages) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-3, "应用平台参数错误！");
		}
		Callable<RSPE<ResultPage<CatDevTeamRebateResp>>> result = new Callable<RSPE<ResultPage<CatDevTeamRebateResp>>>() {
			@Override
			public RSPE<ResultPage<CatDevTeamRebateResp>> call() throws Exception {
				return new RSPE<ResultPage<CatDevTeamRebateResp>>(catDevInviteService.teamList(pid, pages));
			}
		};
		return result;
	}
}
